package com.masterclient.app.modules.category.contract;

import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.modules.category.beans.CategoryDetailBean;
import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 作者：sean
 * 创建时间：2017/7/3 18:06
 * 功能说明：
 */
public class CategoryDetailPresenter extends CategoryDetailContract.Presenter {

    @Override
    public void getBrandGoodList(boolean changeBrand,Map<String,String> attributes,String top_cid, String type,String cid,
                                 String bid, List<String> brand_ids,List<String> category_ids , String start_price, String end_price,
                                 String page, String page_size, String sort, String promotion_type) {

        Gson gson = new Gson();
        String attrs = gson.toJson(attributes);
        StringBuilder brands = new StringBuilder();
        if(brand_ids!=null){
            for(String brand:brand_ids){
                if(brands.length()==0){
                    brands.append(brand);
                }else {
                    brands.append(","+brand);
                }
            }
        }

        StringBuilder categorys = new StringBuilder();
        if(category_ids !=null){
            for(String category:category_ids){
                if(categorys.length()==0){
                    categorys.append(category);
                }else {
                    categorys.append(","+category);
                }
            }
        }

        String sortOption = "";
        if("new".equals(sort)){
            sort = "";
        }else {
            sortOption = sort;
        }
//        Log.e("======", "getBrandGoodList: " + attrs + "=categorys=" + categorys + "=brands=" + brands);
        Observable<CommonBaseResponse<CategoryDetailBean>> observable = RetrofitUtil.getInstance().getCategoryDetail(NetEnvManager.BASE_SURL+"1/category/search_product_list.html",top_cid,type,cid,bid,page,page_size,attrs, brands.toString(),categorys.toString(),sort,promotion_type,start_price,end_price);
        Disposable disposable = toSubscribe(observable,new Consumer<CommonBaseResponse<CategoryDetailBean>>(){

            @Override
            public void accept(CommonBaseResponse<CategoryDetailBean> bean) throws Exception {
                if (bean != null){
                    mView.showSearchGood(bean.getData(),changeBrand);
                }else {
                    mView.searchError();
                }
            }
        },new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.searchError();
            }
        });
        mRxManager.add(disposable);

//        Observable observable = RetrofitUtil.getInstance().getSearchGoodList(Config.BASE_SURL+"1/search/search",attrs,brands.toString(),cids,start_price,end_price,kw,page, page_size,sortOption,promotion_type);
//        Disposable disposable = toSubscribe(observable, new Consumer<SearchGoodBean>() {
//            @Override
//            public void accept(@NonNull SearchGoodBean bean) throws Exception {
//                if(bean!=null && bean.getData()!=null){
//                    mView.showSearchGood(bean.getData(),changeBrand);
//                }else {
//                    mView.searchError();
//                }
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(@NonNull Throwable o) throws Exception {
//                o.printStackTrace();
//                mView.searchError();
//            }
//        });
//        mRxManager.add(disposable);
    }

//    @Override
//    public void getProductList(String brand_id, String cid, String page, String page_size, String sort) {
//        Observable<SearchGoodBean> observable
//                = RetrofitUtil.getInstance().getProductList(Config.BASE_SURL+"1/category/get_product_list/cid_"+cid+"/brand_id_"+brand_id+"/sort_"+sort+"/"+page+"-"+page_size+".html");
//        Disposable disposable = toSubscribe(observable, new Consumer<SearchGoodBean>() {
//            @Override
//            public void accept(@NonNull SearchGoodBean bean) throws Exception {
//                if(bean != null && bean.getData()!= null){
//                    mView.showSearchGood(bean.getData(),true);
//                }
//            }
//        },new ThrowableConsumer(){
//            @Override
//            public void accept(@NonNull Throwable o) throws Exception {
//                super.accept(o);
//            }
//        });
//        mRxManager.add(disposable);
//    }
//
//
//    @Override
//    public void getCategoryBrandList(@android.support.annotation.NonNull String cid) {
//        Observable<CategoryBrandBean> observable = RetrofitUtil.getInstance().getCategoryBrandList(Config.BASE_SURL+"1/category/get_brand_list/cid_"+cid+".html");
//        Disposable disposable = toSubscribe(observable, new Consumer<CategoryBrandBean>() {
//            @Override
//            public void accept(@NonNull CategoryBrandBean bean) throws Exception {
//                if(bean != null && bean.getData()!= null){
//                    mView.showBrandList(bean.getData());
//                }
//            }
//        },new ThrowableConsumer(){
//            @Override
//            public void accept(@NonNull Throwable o) throws Exception {
//                super.accept(o);
//            }
//        });
//        mRxManager.add(disposable);
//    }

}
